package imPro;

import ij.ImagePlus;
import ij.measure.ResultsTable;
import ij.plugin.filter.ParticleAnalyzer;
import ij.process.BinaryProcessor;
import ij.process.ByteProcessor;
import ij.process.FloatProcessor;
import ij.process.ImageProcessor;
import imPro.helpers.Colorspace;
import imPro.helpers.EntropyThreshold;

/**
 *
 * @author Ragnar
 */
public class ProcessImage implements Runnable
{
    private ImPanel panel;
    private Common source;
    
    public ProcessImage(Common src)
    {
        this.source = src;
        Thread t = new Thread(this);
        t.start();
    }
    
    public ProcessImage(ImPanel ip, Common src)
    {
        this.panel = ip;
        this.source = src;
        Thread t = new Thread(this);
        t.start();
    }
    
    public void run()
    {
        while (true)
        {
            if (this.source.getImage() != null)
            {
                ImagePlus i = new ImagePlus("",this.source.getImage());
                Colorspace cp = new Colorspace(i.getProcessor());
                Object[] pixelarray = cp.getLab();
                int w = i.getWidth();
                int h = i.getHeight();

                ImageProcessor ip = new FloatProcessor(w, h, (float[])pixelarray[1], null).convertToByte(true);
                EntropyThreshold et = new EntropyThreshold(ip);
                BinaryProcessor bp = new BinaryProcessor((ByteProcessor)ip);
                bp.invert();
                ResultsTable rt = new ResultsTable();
                ParticleAnalyzer pa = new ParticleAnalyzer(8,            //int options - a flag word created by Oring SHOW_RESULTS, EXCLUDE_EDGE_PARTICLES, etc.
                            65,                                          //int measurements - a flag word created by ORing constants defined in the Measurements interface
                            rt,                                          // rt - a ResultsTable where the measurements will be stored
                            200,                                         //double minSize - the smallest particle size in pixels
                            30000,                                        //double maxSize - the largest particle size in pixels
                            0.3,                                          //minCirc - minimum circularity
                            1);                                             //maxCirc - maximum circularity
                pa.analyze(new ImagePlus("",bp));
                double area = 0;
                double xpos = 0;
                double ypos = 0;
                try
                {
                    area = rt.getValueAsDouble(0, 0); 
                    xpos = rt.getValueAsDouble(8, 0); 
                    ypos = rt.getValueAsDouble(9, 0);

                }
                catch(Exception ex) {}

                LabeledObject lo = new LabeledObject((int) area, (int) xpos, (int) ypos);
                if (this.panel != null)
                {
                    this.panel.setLabeledObject(lo);
                    this.panel.setImage(this.source.getImage());
                }
              //  System.out.println("Areal2: " +(int)area+". Areal: "+area+". X-posisjon: "+xpos+". y-posisjon: "+ypos);
            }
        }
    }
}
